package com.ht.medicaltreatment.viewmodel

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.ToastUtils
import com.ht.medicaltreatment.api.RetrofitUtils
import com.ht.medicaltreatment.bean.*
import com.xgst.driverlesscars.api.ApiService
import com.xgst.lib.http.ApiResult
import kotlinx.coroutines.launch

class ReturnRecordViewModel:BaseViewModel() {

    //模糊查询 正常 归还订单
    fun queryReturnOrder(projectId : String,
                         returnTableName : String,
                         returnTimeEnd : String,
                         returnTimeStart : String,
                         sampleName : String,
                         onSuccess:(MutableList<ReturnRecordBean>?)->Unit){
        viewModelScope.launch {
            val bean = mApi.queryReturnOrder(projectId,returnTableName,returnTimeEnd,
                returnTimeStart,sampleName)
            when(bean){
                is ApiResult.Success -> onSuccess(bean.data.data)
                is ApiResult.Error -> dismissProgress(bean.errorMessage)
            }
        }
    }

    //模糊查询 超时 归还订单
    fun queryReturnTimeOutOrder(projectId : String, onSuccess:(MutableList<ReturnTimeOutRecordBean>?)->Unit){
        viewModelScope.launch {
            val bean = mApi.queryReturnTimeOutOrder(projectId)
            when(bean){
                is ApiResult.Success -> onSuccess(bean.data.data)
                is ApiResult.Error -> dismissProgress(bean.errorMessage)
            }
        }
    }
    //项目
    fun projects(onResult:(MutableList<ProjectBean>)->Unit){
        viewModelScope.launch {
            val bean = mApi.projects()
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error ->  ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

    fun returnOrderDetail(id:Int,projectId:String,onResult:(MutableList<ReturnRecordDetailBean>?)->Unit){
        viewModelScope.launch {
            val bean = mApi.returnOrderDetail(id,projectId)
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data)
                is ApiResult.Error ->  dismissProgress(bean.errorMessage)
            }
        }
    }

    fun returnTimeOutOrderDetail(id:String,value:Int,
                                 onResult:(MutableList<ReturnRecordTimeOutDetailBean>?)->Unit){
        viewModelScope.launch {
            val bean = mApi.returnTimeOutOrderDetail("$id","$value")
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data)
                is ApiResult.Error ->  dismissProgress(bean.errorMessage)
            }
        }
    }
}